{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "79c8c193",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欧拉函数 phi(15): 8\n"
     ]
    }
   ],
   "source": [
    "def prime_factors(n):\n",
    "    factors = []\n",
    "    p = 2\n",
    "    while p * p <= n:\n",
    "        while n % p == 0:\n",
    "            factors.append(p)\n",
    "            n //= p\n",
    "        p += 1\n",
    "    if n > 1:\n",
    "        factors.append(n)\n",
    "    return factors\n",
    "\n",
    "def euler_phi(n):\n",
    "    result = n\n",
    "    factors = prime_factors(n)\n",
    "\n",
    "    for p in set(factors):\n",
    "        result -= result // p\n",
    "\n",
    "    return result\n",
    "\n",
    "# 示例\n",
    "n = 15\n",
    "print(f\"欧拉函数 phi({n}): {euler_phi(n)}\")\n",
    "# 欧拉函数 phi(15): 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "497119ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
